<%@ page contentType="text/html; charset=utf-8"%>
<%@ page import="java.util.Date,java.text.SimpleDateFormat" %>
<%@ include file="/WEB-INF/jsp/includeTaglib.jsp"%>
<%SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");%>
<c:set var="nowDate" value="<%=sdf.format(new Date())%>"></c:set>


<input type="button" value="+添加任务" class="btnStyle" onclick="addTask();" />

<table style="margin: 5px;" width="100%" cellspacing="1" cellpadding="0" border="0" class="dbborder globalText">
    <tr class="rowHead">
        <td height="20" nowrap align="center" class="globalTextBold"></td>
        <td height="20" nowrap align="center" class="globalTextBold">任务</td>
        <td height="20" nowrap align="center" class="globalTextBold">开始时间</td>
        <td height="20" nowrap align="center" class="globalTextBold">截止时间</td>
    </tr>
    <c:forEach var="job" items="${jobs}" varStatus="status">
        <tr class="rowClass">
            <td height="20" class="globalText" width="100">
                <input type="button" class="btnStyle" value="删除" onclick="javascript:deleteCriticalJob(${job.id})">
            </td>
            <td height="20" class="globalText">${job.job.name}</td>
            <td height="20" class="globalText"><fmt:formatDate value="${job.startTime}" pattern="yyyy-MM-dd"/></td>
            <td height="20" class="globalText"><fmt:formatDate value="${job.endTime}" pattern="yyyy-MM-dd"/></td>
        </tr>
    </c:forEach>
</table>
<form id="importTaskForm" >
	<table style="margin: 5px;" width="100%" cellspacing="1" cellpadding="0" border="0" class="dbborder globalText">
		<tbody id="task_list">
        </tbody>
	</table>
</form>

<form target="tempFrame" id="importTaskSubmitForm" action="<c:url value="/project/saveProjectCriticalJob.html" ></c:url>" method="post" >
	<input id="projectCriticalJobData" name="projectCriticalJobData" type="hidden" />
	<input name="projectId" value="${projectId}" type="hidden" />
</form>

<form id="deleteCriticalJobForm" action="<c:url value="/project/deleteProjectCriticalJob.html"></c:url>" method="post" target="tempFrame">
    <input id="criticalJobId" name="criticalJobId" type="hidden">
</form>

<div align="center">
	<input onclick="javascript:window.history.back();" type="button" class="btnStyle" value=" 返回  ">
	<input onclick="doSubmitForm();" type="button" class=" btnStyle" value="保存" />
</div>


<div style="display: none;">
		<table>
			<tr id="cloneTr">
				<td height="20" class="taskNum dynamic globalText">任务</td>
				<td height="20" class="globalText">
					<input readonly="readonly" id="job" type="text" class="required"></td>
				<td height="20" class="globalText">开始时间</td>
				<td height="20" class="globalText">
					<input id="starTime" type="text" style="width: 90px;" class="required">
				</td>
				<td height="20" class="globalText">截止时间</td>
				<td height="20" class="globalText">
					<input id="endTime" type="text" style="width: 90px;" class="required">
				</td>
				<%--<td height="20" class="globalText">--%>
					<%--<input id="delBtn" type="button" class="btnStyle" value="删  除" >--%>
				<%--</td>--%>
			</tr>
		</table>
</div>


<script>
	var index=0;

	function addTask(){
		index++;
		var tr=$("#cloneTr").clone(true);
		tr.attr("id","tr_"+index);

		var starTime=tr.find("#starTime").attr("id","starTime_"+index);
		var endTime=tr.find("#endTime").attr("id","endTime_"+index);
		starTime.bind("click",function(){
			WdatePicker({minDate:'${nowDate}',dateFmt:'yyyy-MM-dd'});
		});
		endTime.bind("click",function(){
			WdatePicker({dateFmt:'yyyy-MM-dd',minDate:'#F{$dp.$D(\''+starTime.attr("id")+'\')}'})
		});

		var job=tr.find("#job");
		job.bind("click",function(){
			chooseTask(index,this);
		});

		tr.find("#delBtn").bind("click",function(){
			tr.remove();
		});


		$("#task_list").append(tr);
	}

	function chooseTask(taskIndex,obj){
		var a=window.showModalDialog('../admin/jobList.html?projectId=${projectId}',window,'status:no;resizable:no;directories:no;help:no;scroll:no;dialogWidth=800px;dialogHeight=450px;dialogLeft:500px;dialogTop:300px;');
		$(obj).val(a.jobName);
		$(obj).attr("jobId",a.jobId);
	}

	function jobListOnloadCallBack(subWindow){
		var subDocument=subWindow.document;
		$(subDocument).find('.delBtn').each(function(i){
			$(this).hide();
		});
		$(subDocument).find('.chooseBtn').each(function(i){
			$(this).show();
			$(this).bind("click",function(){
				subWindow.parent.window.returnValue={jobName:$(this).attr("jobName"),jobId:$(this).attr("jobId")};
				subWindow.close();
			});
		});
		$(subDocument).find('.editBtn').each(function(i){
			$(this).hide();
		});
	}


	function doSubmitForm(){
		assignNameForNull();
		if(!$("#importTaskForm").valid()){
			return;
		}

		var taskList=$('#task_list').children();
		if(taskList.length<1){
			return;
		}

		var submitData=new Array();
		taskList.each(function(){
			var startTime=$(this).find("input[id^='starTime']").val();
		    var endTime=$(this).find("input[id^='endTime']").val();
			submitData.push({
				jobId:$(this).find("#job").attr("jobId"),
				projectId:'${projectId}',
				startTime:startTime,
				endTime:endTime
			});
		});


		$("#projectCriticalJobData").val(JSON.stringify(submitData));
		$("#importTaskSubmitForm").submit();
	}

	function submitCallbackSuccess(obj){
		tabClick($("#rootTab"));
	}

    function deleteCriticalJob(criticalJobId) {
        if(confirm('确认删除项目重点任务')){
            $('#criticalJobId').val(criticalJobId);
            $('#deleteCriticalJobForm').submit();
        }
    }
</script>

<script>
$().ready(function() {
		var validate=$("#importTaskForm").validate({
			showErrors: customShowErrors,
			onfocusout:false,
			onkeyup:false,
			onclick:false
		});
	});
</script>



